﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;

using Alicanto.Helpers;
using Alicanto.Lexing;

namespace Alicanto.Compilation.Code.Operators.Comparison
{
    internal class OpEqual : ICodeGenerator
    {
        public Expression Code { get; private set; }

        public IEnumerable<ParseInstruction> Generate(TokenSequence tokens, Scope scope)
        {
            GetEvaluation left = new GetEvaluation();
            yield return left;

            ParseExpressionRemainder right = new ParseExpressionRemainder();
            yield return right;

            Expression x = left.Expression;
            Expression y = right.Remainder;

            // !IMPORTANT
            Conversion.Equalize(ref x, ref y);

            if (Typing.IsNumber(x.Type) && Typing.IsNumber(y.Type))
                Conversion.EqualizeNumbers(ref x, ref y);

            this.Code = Expression.Equal(
                x,
                y
            );
        }
    }
}
